package org.example.huadianguanli.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.example.base.common.model.TSystemDictItemModel;
import org.example.base.common.service.TsSystemDictService;
import org.example.core.controller.BaseController;
import org.example.core.enums.ApiResult;
import org.example.core.mysql.SearchPageResult;
import org.example.core.mysql.Where;
import org.example.huadianguanli.model.TShopModel;
import org.example.huadianguanli.model.TStorageShopModel;
import org.example.huadianguanli.service.TShopService;
import org.example.huadianguanli.service.TStorageShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Description:
 * Date: 2024/2/14 23:24
 */
@RestController
@RequestMapping("/shop")
public class TShopController extends BaseController<String, TShopService, TShopModel> {

    @Autowired
    private TsSystemDictService tsSystemDictService;

    @Autowired
    private TShopService tShopService;

    @Autowired
    private TStorageShopService tStorageShopService;

    @Override
    public ApiResult searchPage(@RequestParam(defaultValue = "1") int page,
                                @RequestParam(defaultValue = "10") int limit,
                                @RequestParam(required = false) Map<String, Object> params) {
        Where where = Where.build().field("1").equal(1);
        if (!ObjectUtils.isEmpty(params.get("name"))) {
            where.and().field("name").like(params.get("name"));
        }
        int shopCount = tShopService.count(where);
        List<TShopModel> shopModelList = tShopService.search(where.limit(page, limit));
        List<TSystemDictItemModel> shopTypeDictModelList = tsSystemDictService.getByCode("shop_type");
        Map<String, String> shopTypeMap = shopTypeDictModelList.stream().collect(Collectors.toMap(TSystemDictItemModel::getCode, TSystemDictItemModel::getName));
        List<JSONObject> shopList = shopModelList.stream().map(m -> {
            JSONObject shopJsonObject = JSONUtil.parseObj(m);
            shopJsonObject.set("typeStr", m.getType());
            shopJsonObject.set("shopDiscountPrice", BigDecimal.valueOf(m.getPrice() * m.getDiscount()).setScale(2, BigDecimal.ROUND_CEILING));
            return shopJsonObject;
        }).collect(Collectors.toList());
        SearchPageResult<JSONObject> result = new SearchPageResult<>();
        result.setPage(page);
        result.setLimit(limit);
        result.setTotalCount(shopCount);
        result.setList(shopList);
        return ApiResult.success(result);
    }

    @Override
    public ApiResult getById(String id) {
        TShopModel shopModel = tShopService.getById(id);
        String storageId = shopModel.getStorageId();
        JSONObject resultJsonObject = JSONUtil.parseObj(shopModel);
        List<TStorageShopModel> storageShopModels = tStorageShopService.search(Where.build().field("storage_id").equal(storageId).and().field("del_flag").equal(0).and().field("shop_id").equal(id));
        int count = 0;
        for (int i = 0; i < storageShopModels.size(); i++) {
            TStorageShopModel tStorageShopModel = storageShopModels.get(i);
            count += tStorageShopModel.getCount();
        }
        resultJsonObject.set("count", count);
        return ApiResult.success(resultJsonObject);
    }
}
